<template>
    <el-select v-model="selectedCategory" placeholder="请选择商品类别" clearable @change="handleCategoryChange">
        <el-option v-for="item in categoryList" :key="item.id" :label="item.categoryName" :value="item.id">
            <span style="float: left">{{ item.categoryName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">编码: {{ item.categoryCode }}</span>
        </el-option>
    </el-select>
</template>

<script>
import { listCategory } from "@/api/stock/category";

export default {
    name: "CategorySelect",
    props: {
        value: {
            type: [Number, String],
            default: null
        }
    },
    data() {
        return {
            categoryList: [],
            selectedCategory: this.value
        };
    },
    watch: {
        value(val) {
            this.selectedCategory = val;
        }
    },
    created() {
        this.getCategoryList();
    },
    methods: {
        getCategoryList() {
            listCategory().then(response => {
                this.categoryList = response.rows;
            });
        },
        handleCategoryChange(value) {
            this.$emit('input', value);
            this.$emit('change', value);
        }
    }
};
</script>